package cn.rkylin.oms.sysreturn.service.impl;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.common.consts.StatusConsts;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.sysrefund.servcie.impl.SysRefundServiceImpl;
import cn.rkylin.oms.sysreturn.dao.ISysReturnDAO;
import cn.rkylin.oms.sysreturn.dao.ISysReturnSkuDAO;
import cn.rkylin.oms.sysreturn.domain.SysReturn;
import cn.rkylin.oms.sysreturn.domain.SysReturnSku;
import cn.rkylin.oms.sysreturn.service.ISysReturnService;
import cn.rkylin.oms.sysreturn.vo.SysReturnVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 类名:SysReturnServiceImpl <br/>
 * 作用: 退款单服务层实现类. <br/>
 * 创建原因: 退款单服务层实现类. <br/>
 * 创建时间: 2017年9月26日 下午8:48:45 <br/>
 *
 * @author zhanghao
 * @version v1.0
 */
@Service("sysReturnService")
public class SysReturnServiceImpl extends ApolloService implements ISysReturnService {
    private static final Log logger = LogFactory.getLog(SysRefundServiceImpl.class);
    /**
     * sysReturnDAO:退货单数据层对象.
     */
    @Autowired
    ISysReturnDAO sysReturnDAO;
    /**
     * sysReturnSkuDAO:退货单明细数据层对象.
     */
    @Autowired
    ISysReturnSkuDAO sysReturnSkuDAO;

    /**
     * 查询退款单列表.
     *
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#querySysReturnVOList(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public List<SysReturn> querySysReturnVOList(SysReturnVO sysReturnVO) throws Exception {
        List<SysReturn> sysReturnVOList = new ArrayList<SysReturn>();
        sysReturnVOList = sysReturnDAO.selectBySelectiveValue(sysReturnVO);
        return sysReturnVOList;
    }

    /**
     * 生成退货单和退货单明细.
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#generateReturn(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    @Transactional
    public int generateReturn(SysReturnVO sysReturn) throws Exception {
        int returnValue = 0;

        SysReturnVO record = new SysReturnVO();
        BeanUtils.copyProperties(sysReturn, record);
        record.setReturnStatus(StatusConsts.AWAIT);
        record.setDeleted(YesNoConst.NO);
        record.setSourceId(sysReturn.getSourceId());
        record.setSourceCode(sysReturn.getSourceCode());
        record.setIsAutoCreate(YesNoConst.YES);

        record.setIsOperable(YesNoConst.YES);
        int updateCount = sysReturnDAO.updateBySelective(record);

        if (updateCount == 1) {
            // 存在可操作的自动生成的退货单，且，将不存在于退货单上的商品添加到退货单明细里
            for (SysReturnSku sysReturnSku : sysReturn.getSysReturnSku()) {
                sysReturnSkuDAO.insertNotExistSku(sysReturnSku);
            }
        } else if (updateCount == 0) {
            // 不存在可操作的退货单,新增退货单退货商品明细
            sysReturnDAO.insertSelective(sysReturn);
            for (SysReturnSku sysReturnSku : sysReturn.getSysReturnSku()) {
                sysReturnSkuDAO.insert(sysReturnSku);
            }
        } else {
            logger.error("系统错误，针对一条退款单出现了多条可操作的退货单，不执行任何操作");
        }
        return returnValue;
    }

    /**
     * 退货单更新.
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#updateSysReturn(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public int updateSysReturn(SysReturnVO param) throws Exception {
        return sysReturnDAO.updateBySelective(param);
    }

    /**
     * 废弃三方仓退货单.
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#cancelSysReturn4Ext(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public int cancelSysReturn4Ext(SysReturnVO param) throws Exception {
        return sysReturnDAO.cancelSysReturn4Ext(param);

    }

    /**
     * 废弃本地仓退货单
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#cancelSysReturn4Local(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public int cancelSysReturn4Local(SysReturnVO param) throws Exception {
        return sysReturnDAO.cancelSysReturn4Local(param);
    }

    /**
     * 根据自动创建的退货单ID删除退货单明细.
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#deleteReturnSkuByAutoReturnId(java.lang.String)
     */
    @Override
    public int deleteReturnSkuByAutoReturnId(String returnId) throws Exception {
        return sysReturnSkuDAO.deleteByAutoSysReturnID(returnId);
    }

    /**
     * 根据自动创建的退货单ID删除退货单明细.
     *
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#deleteReturnSkuByIdList(java.util.List)
     */
    @Override
    public int deleteReturnSkuByIdList(Set<String> returnSkuIdList) throws Exception {
        int returnValue = 0;
        Iterator<String> idIterator = returnSkuIdList.iterator();
        while(idIterator.hasNext()){
            String returnSkuId = idIterator.next();
            sysReturnSkuDAO.deleteBySysReturnSkuID(returnSkuId);
        }
        return returnValue;
    }

    /**
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#updateUnApproveSysReturn(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public int updateUnApproveSysReturn(SysReturnVO param) throws Exception {
        return sysReturnDAO.updateUnApproveSysReturn(param);
    }

    /**
     * @throws Exception
     * @see cn.rkylin.oms.sysreturn.service.ISysReturnService#querySysReturnVOListWithExtend(cn.rkylin.oms.sysreturn.vo.SysReturnVO)
     */
    @Override
    public List<SysReturnVO> querySysReturnVOListWithExtend(SysReturnVO queryParam) throws Exception {
        List<SysReturnVO> sysReturnVOList = new ArrayList<SysReturnVO>();
        sysReturnVOList = sysReturnDAO.selectBySelectiveValueExtend(queryParam);
        return sysReturnVOList;
    }

    /**
     * 通过来源ID获取系统退货单
     *
     * @param sourceId
     * @return
     * @throws Exception
     */
    @Override
    public List<SysReturn> selectSysReturnForSysOrder(String sourceId) throws Exception {
        return sysReturnDAO.selectSysReturnForSysOrder(sourceId);
    }

    /**
     * querySysReturnVOByRefundId 根据退款单获取退货单列表
     *
     * @param refundId
     * @return
     * @throws Exception
     * @author zhanghao
     */
    @Override
    public List<SysReturnVO> querySysReturnVOByRefundId(String refundId) throws Exception {
        return sysReturnDAO.selectByRefundId(refundId);
    }
}
